package com.ruoyi.funding.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.funding.domain.FundingAllocationCopy;
import com.ruoyi.funding.domain.FundingSource;
import com.ruoyi.funding.service.IFundingAllocationCopyService;
import com.ruoyi.funding.service.IFundingSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.funding.domain.FundingAllocation;
import com.ruoyi.funding.service.IFundingAllocationService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

import static com.ruoyi.common.security.utils.SecurityUtils.getUsername;

/**
 * 资金拨款记录Controller
 * 
 * @author ycl
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/allocation")
public class FundingAllocationController extends BaseController
{
    @Autowired
    private IFundingAllocationService fundingAllocationService;

    @Autowired
    private IFundingSourceService fundingSourceService;
    @Autowired
    private IFundingAllocationCopyService fundingAllocationCopyService;

    /**
     * 查询资金拨款记录列表
     */
    @RequiresPermissions("funding:allocation:list")
    @GetMapping("/list")
    public TableDataInfo list(FundingAllocation fundingAllocation)
    {
        startPage();
        List<FundingAllocation> list = fundingAllocationService.selectFundingAllocationList(fundingAllocation);

        for (FundingAllocation allocation :list){
            Long sourceId = allocation.getSourceId();
            FundingSource fundingSource = fundingSourceService.selectFundingSourceBySourceId(sourceId);
            if(fundingSource!=null)
                allocation.setSourceName(fundingSource.getSourceName());
        }

        return getDataTable(list);
    }

    /**
     * 导出资金拨款记录列表
     */
    @RequiresPermissions("funding:allocation:export")
    @Log(title = "资金拨款记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FundingAllocation fundingAllocation)
    {
        List<FundingAllocation> list = fundingAllocationService.selectFundingAllocationList(fundingAllocation);
        ExcelUtil<FundingAllocation> util = new ExcelUtil<FundingAllocation>(FundingAllocation.class);
        util.exportExcel(response, list, "资金拨款记录数据");
    }

    /**
     * 获取资金拨款记录详细信息
     */
    @RequiresPermissions("funding:allocation:query")
    @GetMapping(value = "/{allocationId}")
    public AjaxResult getInfo(@PathVariable("allocationId") Long allocationId)
    {
        return success(fundingAllocationService.selectFundingAllocationByAllocationId(allocationId));
    }

    /**
     * 新增资金拨款记录
     */
    @RequiresPermissions("funding:allocation:add")
    @Log(title = "资金拨款记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FundingAllocation fundingAllocation)
    {

        return toAjax(fundingAllocationService.insertFundingAllocation(fundingAllocation));


    }

    /**
     * 修改资金拨款记录
     */
    @RequiresPermissions("funding:allocation:edit")
    @Log(title = "资金拨款记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FundingAllocation fundingAllocation)
    {

        return toAjax(fundingAllocationService.updateFundingAllocation(fundingAllocation));
    }

    /**
     * 删除资金拨款记录
     */
    @RequiresPermissions("funding:allocation:remove")
    @Log(title = "资金拨款记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{allocationIds}")
    public AjaxResult remove(@PathVariable Long[] allocationIds)
    {


        return toAjax(fundingAllocationService.deleteFundingAllocationByAllocationIds(allocationIds));
    }

    /**
     * 查询资金拨款记录的历史记录,溯源
     */
    @RequiresPermissions("funding:allocation:history")

    @GetMapping("/history/{allocationId}")
    public AjaxResult history(@PathVariable("allocationId") Long allocationId)
    {
        if (allocationId == null) {
            return AjaxResult.error("参数无效");
        }

        List<FundingAllocationCopy> historyList = fundingAllocationCopyService.selectFundingAllocationCopyByAllocationId(allocationId);
        return AjaxResult.success(historyList);
    }

    //回溯数据
    @PutMapping("/restoreData")
    public AjaxResult restoreData(@RequestBody FundingAllocationCopy fundingAllocationCopy)
    {
        FundingAllocation fundingAllocation =  new FundingAllocation();
        BeanUtils.copyProperties(fundingAllocationCopy,fundingAllocation);
        return toAjax(fundingAllocationService.updateFundingAllocation1(fundingAllocation));
    }



}
